// 将菜单栏的所有数据全部渲染到当前文件
export const menuData = [
    {
        id: "1",
        label: "首页",
        children: [
            {
                id: "11",
                label: "首页",
                path: "/HomeIndex/HomePage"
            }
        ]
    },
    {
        id: "2",
        label: "商品管理",
        children: [
            {
                id: "21",
                label: "商品列表",
                path: "/HomeIndex/GoodsList"
            },
            {
                id: "22",
                label: "分类搜索",
                path: "/HomeIndex/GoodsCategory"
            }
        ]
    },
    {
        id: "3",
        label: "轮播图管理",
        children: [
            {
                id: "31",
                label: "轮播图列表",
                path: "/HomeIndex/BanList",
                children: [
                    {
                        id: '31-C',
                        label: '修改操作人员'
                    },
                    {
                        id: '31-D',
                        label: '删除操作人员'
                    }
                ]
            },
            {
                id: "32",
                label: "添加轮播图",
                path: "/HomeIndex/BanAdd"
            }
        ]
    },
    {
        id: "4",
        label: "操作人员管理",
        children: [
            {
                id: "41",
                label: "操作人员列表",
                path: "/HomeIndex/AdminList",
                children: [
                    {
                        id: '41-C',
                        label: '修改操作人员'
                    },
                    {
                        id: '41-D',
                        label: '删除操作人员'
                    }
                ]
            },
            {
                id: "42",
                label: "添加操作人员",
                path: "/HomeIndex/AdminAdd",
            }
        ]
    },
    {
        id: "5",
        label: "购物车管理",
        children: [
            {
                id: "51",
                label: "购物车列表",
                path: "/HomeIndex/ShoppingList"
            }
        ]
    },
    {
        id: "6",
        label: "用户信息",
        children: [
            {
                id: "61",
                label: "用户列表",
                path: "/HomeIndex/UserList"
            }
        ]
    },
    {
        id: "7",
        label: "订单信息",
        children: [
            {
                id: "71",
                label: "订单列表",
                path: "/HomeIndex/OrderList",
                children: [
                    {
                        id: '71-C',
                        label: '查看订单'
                    },
                    {
                        id: '71-D',
                        label: '修改订单状态'
                    },
                ]
            }

        ]
    },
    {
        id: "8",
        label: "数据图表",
        children: [
            {
                id: "81",
                label: "简单K线图",
                path: "/HomeIndex/ChartKLine"
            },
            {
                id: "82",
                label: "简单图表数据",
                path: "/HomeIndex/ChartData"
            }
        ]
    }
]
// 通过全部菜单数据结合当前用户的权限生成新的菜单数据
export default function curMenu() {
    // 定义一个空的权限表用来接收
    let checkedKeys: string[] = []
    // 判断本地是否有权限
    let current = sessionStorage.getItem('checkedkeys')
    if (current) {
        checkedKeys = current.split(",")
        // checkedKeys = JSON.parse(current)
        console.log(checkedKeys)
    }
    // 当用户登录时，本地存储的用户权限为空，则给该用户赋予全部权限
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '11', '2', '21', '22', '3', '31', '32', '4', '41', '42', '5', '51', '6', '61', '7', '71', '8', '81', '82']
    }
    // 创建一个空的菜单列表用来接收用户的权限菜单
    let currentMenu: any[] = []
    // 遍历全部菜单列表，进行过滤
    menuData.forEach((item) => {
        // 判断用户权限是否在全部菜单的权限中
        if (checkedKeys.includes(item.id)) {
            // push时使用解构赋值，不会影响空间中的其他值
            currentMenu.push({ ...item })
            // const newMenu={...item}
            // 再次进行判断item是否有children
            if (item.children) {
                // 如果children存在，就先将用户权限菜单中的children给定为一个空数组，
                currentMenu[currentMenu.length - 1].children = []
                // 对children进行遍历
                item.children.forEach((sonItem) => {
                    // 判断用户权限中是否有children中的权限
                    if (checkedKeys.includes(sonItem.id)) {
                        // 有的话
                        currentMenu[currentMenu.length - 1].children.push({ ...sonItem })
                    }
                })
            }
        }
    })
    return currentMenu
}